﻿using EnglishAuction.BUS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EnglishAuction
{
    public partial class ManageAccount : System.Web.UI.Page
    {
        private int? EditUserId
        {
            get
            {
                return (int?)ViewState["EditUserId"];
            }

            set
            {
                ViewState["EditUserId"] = value;
            }
        }

        private void Clear()
        {
            txtTenTaiKhoan.Enabled = true;
            txtTenTaiKhoan.Text = "";
            txtMatKhau.Text = "";
            txtError.Text = "";
            txtHoTen.Text = "";
            txtSoDienThoai.Text = "";
            txtDiaChi.Text = "";
        }

        private void FillData(int id)
        {
            NhanVien nhanVien = NhanVienBUS.Get(id);
            txtAccount.Enabled = false;
            txtAccount.Text = nhanVien.TaiKhoan;
            txtPhoneNumber.Text = nhanVien.SoDienThoai;
            txtAddress.Text = nhanVien.DiaChi;
            txtFullName.Text = nhanVien.HoTen;
            if (nhanVien.GioiTinh.Value)
                rdMale.Checked = true;
            else
                rdFemale.Checked = true;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            txtError.Text = "";
            if (!IsPostBack)
            {
                var lst = LoaiNhanVienBUS.GetAll();
                lst.Insert(0, new LoaiNhanVien() { Id = -1, LoaiNhanVien1 = "  --Chọn loại nhân viên--  " });
                cboLoaiNhanVien.DataSource = lst;
                cboLoaiNhanVien.DataTextField = "LoaiNhanVien1";
                cboLoaiNhanVien.DataValueField = "Id";
                cboLoaiNhanVien.DataBind();
                gvCustomers.DataSource = NhanVienBUS.GetAll();
                gvCustomers.DataBind();             
            }
        }

        protected void btnAddCustomer_Click(object sender, EventArgs e)
        {

        }

        protected void btnRefreshGrid_Click(object sender, EventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (LoaiNhanVienBUS.CheckUsername(txtTenTaiKhoan.Text))
            {
                txtError.Text = "Tài khoản đã tồn tại.";
            }
            else
            {
                NhanVien nhanVien = new NhanVien();
                nhanVien.TaiKhoan = txtTenTaiKhoan.Text;
                nhanVien.MatKhau = Utilities.GetMD5Hash(txtMatKhau.Text);
                nhanVien.HoTen = txtHoTen.Text;
                nhanVien.LoaiNhanVienId = int.Parse(cboLoaiNhanVien.SelectedValue);
                nhanVien.SoDienThoai = txtSoDienThoai.Text;
                nhanVien.GioiTinh = rbNam.Checked == true ? true : false;
                nhanVien.DiaChi = txtDiaChi.Text;
                NhanVienBUS.Insert(nhanVien);
                
                RegisterStartupScriptCommand("jsCloseDialg", "closeDialog();");
                TriggerClientGridRefresh();

                txtTenTaiKhoan.Text = "";
                txtMatKhau.Text = "";
                txtHoTen.Text = "";
                txtSoDienThoai.Text = "";
                txtDiaChi.Text = "";
                cboLoaiNhanVien.SelectedIndex = 0;
            }
        }

        private void LoadListEmployee()
        {
            gvCustomers.DataSource = NhanVienBUS.GetAll();
            gvCustomers.DataBind();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            //Clear();
        }

        protected void gvCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem == null)
                return;
            ImageButton btnEdit = (ImageButton)e.Row.FindControl("btnUp");
            btnEdit.OnClientClick = "openDialogAndBlock2('Cập nhật thông tin nhân viên', '" + btnEdit.ClientID + "')";
        }

        protected void gvCustomers_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument);

            switch (e.CommandName)
            {
                //Can't use just Edit and Delete or need to bypass RowEditing and Deleting
                case "EditCustomer":
                    FillData(id);
                    this.EditUserId = id;
                    Thread.Sleep(1000);
                    RegisterStartupScriptCommand("jsUnblockDialog", "unblockDialog2();");
                    break;

                case "DeleteCustomer":
                    NhanVienBUS.Delete(id);
                    gvCustomers.DataSource = NhanVienBUS.GetAll();
                    gvCustomers.DataBind();
                    break;
            }
        }

        private void TriggerClientGridRefresh()
        {
            string script = "__doPostBack(\"" + btnRefreshGrid.ClientID + "\", \"\");";
            RegisterStartupScriptCommand("jsGridRefresh", script);
            LoadListEmployee();
        }

        private void RegisterStartupScriptCommand(string key, string script)
        {
            ScriptManager.RegisterStartupScript(phrJsRunner, phrJsRunner.GetType(), key, script, true);
        }

        protected void btnSave_Click1(object sender, EventArgs e)
        {
            
        }

        protected void btnCancel_Click1(object sender, EventArgs e)
        {

        }

        protected void btnS_Click(object sender, EventArgs e)
        {
            NhanVien nhanVien = NhanVienBUS.Get(EditUserId.Value);
            nhanVien.HoTen = txtFullName.Text;
            nhanVien.SoDienThoai = txtPhoneNumber.Text;
            nhanVien.DiaChi = txtAddress.Text;
            nhanVien.GioiTinh = rdMale.Checked == true ? true : false;
            if (!txtPassword.Text.Equals(""))
            {
                nhanVien.MatKhau = Utilities.GetMD5Hash(txtMatKhau.Text);
            }

            NhanVienBUS.Update(nhanVien);
            RegisterStartupScriptCommand("jsCloseDialg", "closeDialog2();");
            TriggerClientGridRefresh();
        }
    }
}